/*
 * Created on 16/Dez/2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ceirinhashls;

import java.io.*;
import java.net.Socket;
import java.text.ParseException;

/**
 * @author botelhodaniel
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class HubServerThread extends Thread {
	private Socket socket;
	private DataOutputStream out;

	/**
	 * 
	 * @uml.property name="in"
	 * @uml.associationEnd multiplicity="(1 1)"
	 */
	private DCBufferedReader in;

	/**
	 * 
	 * @uml.property name="hub"
	 * @uml.associationEnd multiplicity="(1 1)"
	 */
	private DCHub hub;

	/**
	 * 
	 * @uml.property name="hublist"
	 * @uml.associationEnd multiplicity="(1 1)"
	 */
	private PublicHubList hublist;

	public HubServerThread(Socket socket,PublicHubList hublist) throws IOException {
		super("HubServerThread");
		this.socket = socket;
		this.hublist = hublist;
		this.out = new DataOutputStream(this.socket.getOutputStream());
		this.in = new DCBufferedReader(new InputStreamReader(this.socket.getInputStream()));
		hub = new DCHub(socket.getRemoteSocketAddress());
	}

	public void run() {
		System.out.println("Dentro");
		try {
			String inputLine;
			System.out.println("waiting...");
			out.write("$Lock tHn[0U1d1lewXCi:b=D^DlKR_L3jM6d+bSf;^BX7\\9cZ?dt Pk=X,[oa),]/Z0M(*SB|".getBytes());
			System.out.println("waiting....");

			hub.processComand(in.readLine());
			System.out.println(hub);
			hublist.addDCHub(hub);
			in.close();
			out.close();
			socket.close();
			System.out.println("\tTerminou...");
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
